package util;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

public class ReflectUtil {

    /***
     * 将请求参数中的数据设置到对象属性中
     * @param req
     * @param object
     * @throws IllegalAccessException
     * @throws ParseException
     * @throws SQLException
     */
    public static void httpServletRequestSetToObject(HttpServletRequest req, Object object) throws IllegalAccessException, ParseException, SQLException {
        Class<?> clazz = object.getClass();

        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            //属性名
            System.out.println(field.getName());
            //类型
            System.out.println(field.getType().getName());
            //值
            System.out.println(req.getParameter(field.getName()));

            //暴力反射
            field.setAccessible(true);
            String typeName = field.getType().getName();

            if (null != req.getParameter(field.getName())) {
                if ("long".equals(typeName) || "java.lang.Long".equals(typeName)) {
                    Long logValue = Long.valueOf(req.getParameter(field.getName()));
                    field.set(object, logValue);
                } else {
                    field.set(object, req.getParameter(field.getName()));
                }
            }
        }
    }

    /***
     * 将结果集 ResultSet 的值设置到对象中
     * @param rs
     * @param object
     * @throws IllegalAccessException
     * @throws ParseException
     * @throws SQLException
     */
    public static void resultSetToObject(ResultSet rs, Object object) throws IllegalAccessException, SQLException {
        Class<?> clazz = object.getClass();

        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            //属性名
            System.out.println(field.getName());
            //类型
            System.out.println(field.getType().getName());
            //值
            System.out.println(rs.getObject(field.getName()));

            //暴力反射
            field.setAccessible(true);
            String typeName = field.getType().getName();

            try {
                if (null != rs.getObject(field.getName())) {
                    if ("long".equals(typeName) || "java.lang.Long".equals(typeName)) {
                        Long logValue = Long.valueOf(rs.getLong(field.getName()));
                        field.set(object, logValue);
                    } else if ("java.sql.Timestamp".equals(typeName)) {
                        Timestamp timestamp = rs.getTimestamp(field.getName());
                        // 如果不希望为 null 的 Timestamp 字段设置 null 值，则可以在这里添加额外的逻辑
                        field.set(object, timestamp);
                    } else {
                        field.set(object, rs.getObject(field.getName()));
                    }
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public static void mapToObject(Map<String, String> map, Object object) throws IllegalAccessException, ParseException {
        Class<?> clazz = object.getClass();
        Field[] fields = clazz.getDeclaredFields();

        for (Field field : fields) {
            //属性名
            System.out.println(field.getName());
            //类型
            System.out.println(field.getType().getName());
            //值
            System.out.println(map.get(field.getName()));

            //暴力反射
            field.setAccessible(true);
            String typeName = field.getType().getName();

            if (null != map.get(field.getName())) {
                if ("long".equals(typeName) || "java.lang.Long".equals(typeName)) {
                    Long logValue = Long.valueOf(map.get(field.getName()));
                    field.set(object, logValue);
                } else if ("java.math.BigDecima".equals(typeName)) {
                    BigDecimal bigDecimal = new BigDecimal(map.get(field.getName()));
                    field.set(object, bigDecimal);
                } else if ("java.util.Date".equals(typeName)) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date date = sdf.parse(map.get(field.getName()));
                    field.set(object, date);
                } else {
                    field.set(object, map.get(field.getName()));
                }
            }
        }
    }
}
